package com.android.hq.ganktoutiao.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.hq.ganktoutiao.R;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

/**
 * Created by Administrator on 2017/5/15.
 */
public class ZhaoHui extends AppCompatActivity {

    Button fanhui;
    TextView number;
    TextView yanzheng;
    Button fasong;
    TextView newpassword1;
    TextView newpassword2;
    Button tijiao;
    Handler handler;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_item_zhaohui);

        fanhui = (Button) findViewById(R.id.button_fanhui);
        number = (TextView) findViewById(R.id.edit_zhaohuiname);
        yanzheng = (TextView) findViewById(R.id.edit_yanzhengma);
        fasong = (Button) findViewById(R.id.button_findyanzhengma);
        newpassword1 = (TextView) findViewById(R.id.edit_newpassword);
        newpassword2 = (TextView) findViewById(R.id.edit_querenpassword);
        tijiao = (Button) findViewById(R.id.button_tijiao);

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZhaoHui.this, ZhuCeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        fasong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberP = number.getText().toString();

                if (numberP.equals(null) || numberP.equals("")) {
                    Toast.makeText(ZhaoHui.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                } else {
                    //  当号码部位空时，将号码发送到服务器进行验证
                    handler = new Handler() {

                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);

                            if (msg.what == 1) {

                                Toast.makeText(ZhaoHui.this, "验证码发送成功", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(ZhaoHui.this, "验证码发送失败", Toast.LENGTH_LONG).show();
                            }
                        }
                    };
                    yanZhengM(numberP);
                }
            }
        });

        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberP = number.getText().toString();
                String yanzhengM = yanzheng.getText().toString();
                String passwordO = newpassword1.getText().toString();
                String passwordT = newpassword2.getText().toString();

                if ((numberP.equals(null) || numberP.equals("")) || (yanzhengM.equals(null) || yanzhengM.equals(""))) {
                    Toast.makeText(ZhaoHui.this, "号码以及验证码不能为空", Toast.LENGTH_LONG).show();
                } else if ((!numberP.equals(null) || !numberP.equals("")) || (!yanzhengM.equals(null) || !yanzhengM.equals(""))) {

                    if ((passwordO.equals(null) || passwordO.equals("")) || (passwordT.equals(null) || passwordT.equals(""))) {
                        Toast.makeText(ZhaoHui.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    } else {
                        if (!passwordO.equals(passwordT)) {
                            Toast.makeText(ZhaoHui.this, "两次输入密码不统一", Toast.LENGTH_LONG).show();
                        } else if (passwordO.equals(passwordT)) {
                            // 将号码、验证码、密码等发送到服务器进行保存

                            handler = new Handler() {
                                @Override
                                public void handleMessage(Message msg) {
                                    super.handleMessage(msg);

                                    if (msg.what == 1) {
                                        Toast.makeText(ZhaoHui.this, "密码修改成功", Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(ZhaoHui.this, "密码修改失败", Toast.LENGTH_LONG).show();
                                    }
                                }
                            };
                            zhaoHuiMiMa(numberP, yanzhengM, passwordO);
                        }
                    }
                } else {

                }

            }
        });
    }


    //  找回密码获取验证码
    private void yanZhengM (final String usernumber) {


        new Thread() {
            @Override
            public void run() {
                super.run();

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://api.zhaibao.cn/sendResetPwdMobileCode?mobile" + usernumber + "&version=1.0&deviceType=2");

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    } else {
                        Log.d("错误", "验证码发送失败");
                    }

                } catch (IOException e) {
                    Log.d("错误", e.toString());
                }

            }
        }.start();
    }

    //  找回密码
    private void zhaoHuiMiMa (final String usernumber, final String yanzhengma, final String userpassword) {

        new Thread() {

            @Override
            public void run() {
                super.run();

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://api.zhaibao.cn/resetPwd?mobile" + usernumber + "&code=" + yanzhengma + "&password" + userpassword + "&version=1.0&deviceType=2");

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    } else {
                        Log.d("错误", "修改失败");
                    }
                } catch (IOException e) {
                    Log.d("错误", e.toString());
                }
            }
        }.start();

    }
}
